package pers.qianyu.controller;

import cn.hutool.core.map.*;
import io.swagger.annotations.*;
import lombok.extern.slf4j.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;
import pers.qianyu.model.comm.*;
import pers.qianyu.model.dto.*;
import pers.qianyu.model.vo.*;
import pers.qianyu.service.*;

import java.util.*;

@Slf4j
@Api("用户相关接口")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/auth/users")
    @ApiOperation("分页获取用户信息")
    public Results<PageVO<UserVO>> getUsers(
            @ApiParam(name = "页码", required = false)
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam(name = "每页数量", required = false)
            @RequestParam(required = false, defaultValue = "5") Integer limit,
            @RequestAttribute Long userId) {
        log.info("userId : {}", userId);
        return Results.ok(userService.getUsers(page, limit));
    }

    @GetMapping("/auth/user/{id}")
    @ApiOperation("根据id获取用户信息")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String", paramType = "path")
    public Results<UserVO> user(@PathVariable Long id) {
        return Results.ok(userService.getUserById(id));
    }

    @PostMapping("/user")
    @ApiOperation("增加用户")
    public Results register(@ApiParam(name = "用户信息", value = "传入json格式", required = true)
                            @RequestBody UserDTO userDTO) {
        userService.insertUser(userDTO);
        return Results.ok("注册成功", null);
    }

    @DeleteMapping("/auth/user/{id}")
    @ApiOperation("删除用户")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String", paramType = "path")
    public Results deleteUser(@PathVariable Long id) {
        userService.deleteUserById(id);
        return Results.ok("删除成功", null);
    }

    @PutMapping("/auth/user/{id}")
    @ApiOperation("修改用户信息")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String", paramType = "path")
    public Results<Map<String, Object>> putUser(@ApiParam(name = "要修改的用户信息", value = "传入json格式", required = true)
                                                @RequestBody UserDTO userDTO,
                                                @PathVariable Long id) {
        userService.updateUserById(id, userDTO);
        return Results.ok("更新成功", MapUtil.of("id", id));
    }
}
